<?php
/**
 * Conversations - A lightweight BB for Xaraya
 *
 * @package modules
 * @copyright (C) 2003-2006 The Digital Development Foundation.
 * @license GPL {@link http://www.gnu.org/licenses/gpl.html}
 * @link http://www.xaraya.com
 *
 * @subpackage conversations
 * @link http://xaraya.com/index.php/release/760.html
 * @author John Cox
*/
/**
 * Update a lookup
*/

/**
 * Update a visit time
 * @author John Cox
 * @author Jo dalle Nogare
 * @param $args['tagid'] integer
 * @param $args['uid'] integer
 * @returns bool
 * @return true on success, NULL on failure
 */

function conversations_userapi_updatevisits($args)
{
    // Get arguments from argument array
    extract($args);

    // params in arg
    $params = array(
        "tagid"  => "xar_tagid",
        "uid"    => "xar_uid"
    );

    foreach($params as $vvar => $dummy) {
        if (isset($args[$vvar])) {
            $set = true;
            break;
        }
    }

    if (!isset($set)) {
        $invalid[] = xarML('At least one parameter has to be set: #(1)', join(',', array_keys($fields)));
    }
    // Argument check - make sure that at least on paramter is present
    // if not then set an appropriate error message and return
    if (isset($invalid)) {
        $msg = xarML('Invalid parameter count');
        xarErrorSet(XAR_SYSTEM_EXCEPTION, 'BAD_PARAM', new SystemException($msg));
        return;
    }
    $timestamp = time();
    // Check to see if this timestamp exists, and if not, just create it.
    $new = xarModAPIFunc('conversations', 'user', 'getvisits', array(
                                        'tagid'   => $tagid,
                                        'uid'   => $uid));
                                        
    //return var_export($new);
                                        
    if (empty($new)){
        xarModAPIFunc('conversations', 'user', 'createvisits', array(
                                        'tagid'   => $tagid,
                                        'uid'   => $uid,
                                        'timestamp' => $timestamp));
        return true;
    }


    // Get datbase setup
    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['cuvisit'];

    $update = array();
    $bindvars = array();

    // Update item
    $query = "UPDATE $table SET ";
    
    if (isset($timestamp)){
        $update[] = "xar_timestamp = ? ";
        $bind[] = $timestamp;
    }
    
    $query .= join(",",$update);
    
    if (!empty($uid)) {
        $where[] = 'xar_uid = ?';
        $bind[] = (int)$uid;
    }

    if (!empty($tagid)) {
        $where[] = 'xar_tagid = ?';
        $bind[] = (int)$tagid;
    }

    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }
    
    $result =& $dbconn->Execute($query, $bind);
    if (!$result) return;
    return true;
}
?>